module ExportHtpasswd

  def self.export
    Setting.plugin_redmine_export_htpasswd['export_path'].each do |identifier, export_path|
      if export_path == "" then
        next
      end
      project = Project.find(:all,
                             :conditions => ["identifier = ?", identifier],
                             :order => :identifier
      ).first
      members = project.members.find(:all, :include => [:user]).sort

      file = File.open(export_path, 'w')
      file.puts('# Project Info ' + identifier + '(' + project.name + ")\n")
      members.each do |member|
        file.puts(member.user.login + ':{SHA}' + Array[Array[member.user.hashed_password].pack("H*")].pack('m'))
      end
      file.close
    end
  end

end
